package com.ultrpower.test.controller;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.validation.BindException;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.SimpleFormController;

import com.ultrpower.test.bean.User;
import com.ultrpower.test.bean.dataSource;
import com.ultrpower.test.common.BaseFormController;
import com.ultrpower.test.common.BaseRedirectController;
import com.ultrpower.test.util.WebTool;


public class loginController  extends BaseRedirectController {
	/**author jw
	 * @return the customUrl
	 */
	 /**
	 */
	private static Logger logger = Logger.getLogger(loginController.class);
	private String customUrl;
/**
	 * @return the customUrl
	 */
	public String getCustomUrl() {
		return customUrl;
	}
	/**
	 * @param customUrl the customUrl to set
	 */
	public void setCustomUrl(String customUrl) {
		this.customUrl = customUrl;
	}
	//	protected Object formBackingObject(HttpServletRequest request) throws Exception {
//
//		String username = WebTool.getParam(request,"user_name","");
//		String password = WebTool.getParam(request,"user_password","");
//		User user = new User();
//		if("root".equals(username) && "123".equals(password))
//		{
//			List list = dataSource.getData();
//			request.setAttribute("data","notnull");
//			return list;
//		}
//		else
//		{
//		request.setAttribute("null",user);
//		return user;
//		}
//	}
//	protected ModelAndView handleRequestInternal(HttpServletRequest request,
//			HttpServletResponse response,Object command, BindException errors) throws Exception {
//		@SuppressWarnings("unused")
//		User u= (User)command;
//		String username = WebTool.getParam(request,"user_name","");
//		String password = WebTool.getParam(request,"user_password","");
//			if("root".equals(username) && "123".equals(password))
//			{
//				return new ModelAndView(getSuccessView());
//			}
//			return null;
//	}
	public void afterPropertiesSet() throws Exception {
		// TODO Auto-generated method stub	
	}
	@Override
	protected ModelAndView handleRequestInternal(HttpServletRequest pRequest, HttpServletResponse pResponse) throws Exception {
		String username = WebTool.getParam(pRequest,"user_name","");
		String password = WebTool.getParam(pRequest,"user_password","");
		List list = dataSource.getData();
		HttpSession session = (HttpSession)pRequest.getSession(true);
		String pageListSize = (String) session.getAttribute("PAGE_LIST_SIZE");
		if (pageListSize == null) {
			session.setAttribute("PAGE_LIST_SIZE", "10");
			pageListSize = "10";
		}
		if("root".equals(username) && "123".equals(password)){
			pRequest.setAttribute("list",list);
			pRequest.setAttribute("pageListSize",pageListSize);	
			return new ModelAndView(this.customUrl);
		}
		else
		{
			pRequest.setAttribute("list","null");
			return null;
		}
	}

}
